/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.impl;

import com.ivanbiz.dao.HutangDAO;
import com.ivanbiz.model.HibernateUtil;
import com.ivanbiz.model.Hutang;
import com.ivanbiz.model.HutangHistory;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author User
 */
public class HutangDAOImpl extends GenericDAOImpl implements HutangDAO {

    public List getByJatuhTempo(Object date) throws Exception {
        try {
            Session session = HibernateUtil.getSession();
            String query = "from com.ivanbiz.model.Hutang h " +
                    "where h.jatuhTempo <'" + date + "' " +
                    "and h.sisaHutang !=0";
            List list = session.createQuery(query).list();
            return list;
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }

    public List getByBelumLunas() throws Exception {
        try {
            Session session = HibernateUtil.getSession();
            String query = "from com.ivanbiz.model.Hutang h " +
                    "where h.sisaHutang !=0";
            List list = session.createQuery(query).list();
            return list;
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }

    public void bayarHutang(Hutang hutang, double bayar) throws Exception {
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            Date date = new Date();
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
            HutangHistory hutangHistory = new HutangHistory();
            hutangHistory.setHut(hutang);
            hutangHistory.setHutang(hutang.getSisaHutang());
            hutangHistory.setBayar(bayar);
            hutangHistory.setSisa(hutang.getSisaHutang() - bayar);
            hutangHistory.setCreateDate(date);
            session.save(hutangHistory);
            hutang.setSisaHutang(hutangHistory.getSisa());
            session.update(hutang);
            HibernateUtil.commitTransaction();
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }

    public List searchByNota(String nota) throws Exception {
        try {
            Session session = HibernateUtil.getSession();
            String query = "from com.ivanbiz.model.Hutang h " +
                    "where h.sisaHutang !=0 " +
                    "and h.nota.kode=:'" + nota + "'";
            List list = session.createQuery(query).list();
            return list;
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }
}
